const praise = async (event, context) => {

	const { data } = event
	
	const {uid,postID,praiseStatus} = data
	
	const db = uniCloud.database()
	const $ = db.command
	const posts = db.collection("posts")
	
	const post = await posts.doc(postID)
	
	let res = {}
	
	// 如果没有点赞
	if (!praiseStatus){
		await post.update({
			praise_user_ids:$.push(uid)
		})
	}else {
		await post.update({
			praise_user_ids:$.pull(uid)
		})
	}
	res.errCode = 0
	
	return res
}


const love = async (event, context) => {

	const { data } = event
	
	const {uid,postID,loveStatus} = data
	
	const db = uniCloud.database()
	const $ = db.command
	const posts = db.collection("posts")
	
	const post = await posts.doc(postID)
	
	let res = {}
	
	// 如果没有点赞
	if (!loveStatus){
		await post.update({
			love_user_ids:$.push(uid)
		})
	}else {
		await post.update({
			love_user_ids:$.pull(uid)
		})
	}
	res.errCode = 0
	
	return res
}


module.exports =  {
	praise,
	love
}
